package com.ruoyi.common.utils;

import org.springframework.beans.factory.annotation.Value;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESUtils {


    /**
     * 这边写死加密密码
     * 好像只能写死
     */
    private static String BASE64_SECRET = "7190723971907239";

    /**
     * 用来加密解密的byte[]
     */
    private final static byte[] SECRET_BYTES = BASE64_SECRET.getBytes();

    /**
     * 使用aes加密
     *
     * @param content
     * @return
     */
    public static String AESEnCode(String content) throws Exception {
        SecretKeySpec key = new SecretKeySpec(SECRET_BYTES, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedData = cipher.doFinal(content.getBytes());
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    /**
     * 使用aes算法,进行解密
     *
     * @param ciphertext
     * @return
     */
    public static String AESDeCode(String ciphertext) throws Exception {
        SecretKeySpec key = new SecretKeySpec(SECRET_BYTES, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        // 使用Base64解码还原为字节数组
        byte[] originalData = cipher.doFinal(Base64.getDecoder().decode(ciphertext));
        return new String(originalData); // 转换为String格式
    }
}
